package com.webflux.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * 用于比较springmvc和webflux的性能
 *
 * @author: 王霄
 * @date: 2022/1/13 13:19
 **/
@RestController
@RequestMapping("/compare")
public class CompareController {

    private static final String mockUrl = "http://127.0.0.1:8080/client/mock";

    @Resource
    private WebClient webClient;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/mvc")
    public String mvc() {
        return restTemplate.getForObject(mockUrl, String.class);
    }

    @GetMapping("/webflux")
    public Mono<String> webflux() {
        return webClient
                .get()
                .uri(mockUrl)
                .retrieve()
                .bodyToMono(String.class);
    }
}
